/*
 *所有关于PatientReCord类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*PatientReCordService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VoL.Domain.IRepositories;
using VoL.Domain.Infrastructure;
using VoL.Domain.Dtos.PatientReCord;
using VoL.Domain.Repositories;

namespace VoL.Domain.Services
{
    /// <summary>
    /// 实现
    /// </summary>
    public partial class PatientReCordService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IBaseRepository<Patient> _patieRepo;
        private readonly IBaseRepository<Office> _officeRepo;
        private readonly IBaseRepository<PatientReCord> _patientRecordRepo;
        private readonly IPatientReCordRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public PatientReCordService(
            IPatientReCordRepository dbRepository,
            IHttpContextAccessor httpContextAccessor,
            IBaseRepository<Patient> patieRepo,
            IBaseRepository<Office> officeRepo,
            IBaseRepository<PatientReCord> patientRecordRepo
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _patieRepo = patieRepo;
            _officeRepo = officeRepo;
            _patientRecordRepo = patientRecordRepo;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public Task<WebResponseContent> GetPatientReCord(string bedCode)
        {
            var res = new WebResponseContent();

            var patientlist = _patieRepo.GetAll();

            var patientRecordlist = _patientRecordRepo.GetAll();

            var officelist = _officeRepo.GetAll();

            var patientReCordInfoDTOlist = from a in patientRecordlist
                       join b in patientlist on a.PatientCode equals b.PatientCode
                       join c in officelist on b.OfficeCode equals c.OfficeCode
                       where b.BedCode == bedCode
                       select new PatientReCordInfoDTO()
                       {
                           PatientCode = b.PatientCode,
                           Breathe=a.Breathe,
                           Temperature=a.Temperature,
                           Oximetry=a.Oximetry,
                           HeartRate=a.HeartRate,
                           MaxBP=a.MaxBP,
                           MinBP=a.MinBP,
                           Measures=a.Measures,
                           SignUrl=a.SignUrl,
                           Name=b.Name,
                           Age=b.Age,
                           Sex=b.Sex,
                           OfficeName=c.OfficeName,
                           BedCode=b.BedCode
                       };

            res.OK("评估列表查询成功", patientReCordInfoDTOlist);
            return Task.FromResult(res);

        }

        /// <summary>
        /// 新增护理记录单
        /// </summary>
        /// <param name="patientReCord"></param>
        /// <returns></returns>
        public async Task<int> CreatePatientReCord(PatientReCord patientReCord)
        {
            try
            {
                var i = await _patientRecordRepo.Add(patientReCord);
                return i;
            }
            catch (Exception)
            {

                throw;
            }
        }

    }
}
